﻿#include"BitSet.h"
#include"Bloomfilter.h"
void test()
{
	zwy::bit_set<100> bs;
	bs.set(32);
	bs.set(33);
	bs.set(34);
	bs.set(35);
	bs.reset(33);

	bs.set(100);
	bs.set(99);
	cout << bs.test(32) << endl;
	cout << bs.test(33) << endl;

	cout << bs.test(34) << endl;
	cout << bs.test(35) << endl;

	bs.print();

}

void test_three_bitset()
{
	zwy::threebit_set<100> tbs;
	int a[] = { 1,2,2,3,3,3,4,4,4,4,5,5,5,5,5,6,6,6,6,6,6,7,7,7,7,7,7,7,8,8
		,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,99};
	cout << size(a) << endl;
	for (auto e : a)
	{
		tbs.set(e);
	}

	for (size_t i = 0; i < 100; ++i)
	{
		cout << i << "->" << tbs.get_count(i) << endl;
	}
}

void test_bit_set1()
{
	int a1[] = { 5,7,9,2,5,99,5,5,7,5,3,9,2,55,1,5,6 };
	int a2[] = { 5,3,5,99,6,99,33,66 };

	zwy::bit_set<100> bs1;
	zwy::bit_set<100> bs2;

	for (auto e : a1)
	{
		bs1.set(e);
	}

	for (auto e : a2)
	{
		bs2.set(e);
	}

	for (size_t i = 0; i < 100; i++)
	{
		if (bs1.test(i) && bs2.test(i))
		{
			cout << i << endl;
		}
	}
}

void testbf()
{
	Bloomfilter<10> bf;
	bf.set("1111");
	bf.set("2222");
	bf.set("3333");
	bf.set("4444");

	string a[] = { "1112","2232","3331","4344" };
	static size_t n = 0;
	for (size_t i = 0; i < sizeof(a)/sizeof(string); ++i)
	{
		if (bf.test(a[i]))
		{
			++n;
		}
	}
	double rate = n / 4;//误判率
	cout << rate << endl;
}

void TestBloomFilter1()
{
	string strs[] = { "百度","字节","腾讯" };
		Bloomfilter<10> bf;
		for (auto& s : strs)
		{
				bf.set(s);
		}
		for (auto& s : strs)
		{
				cout << bf.test(s) << endl;
		}
		for (auto& s : strs)
		{
				cout << bf.test(s + 'a') << endl;
		}
		cout << bf.test("摆渡") << endl;
		cout << bf.test("百渡") << endl;
			}

void TestBloomFilter2()
{
		srand(time(0));
	const size_t N = 1000000;
	Bloomfilter<N,10> bf;
	//BloomFilter<N, 3> bf;
	//BloomFilter<N, 10> bf;
	vector<string> v1;
	//string url = "https://www.cnblogs.com/clq / archive / 2012 / 05 / 31 / 2528153.html";
		std::string url = "https://www.baidu.com/s?ie=utf8 & f = 8 & rsv_bp = 1 & rsv_idx = 1& tn = 65081411_1_oem_dg & wd = ln2 & fenlei = 256 & rsv_pq = 0x8d9962630072789f & rsv_t = ceda1rulSdBxDLjBdX4484KaopD % 2BzBFgV1uZn4271RV0PonRFJm0i5xAJ % 2FDo & rqlang = en & rsv_enter = 1 & rsv_dl = ib & rsv_sug3 = 3 & rsv_sug1 = 2 & rsv_sug7 = 100 & rsv_sug2 =0 & rsv_btype = i & inputT = 330 & rsv_sug4 = 2535";
		//std::string url = "猪八戒";
		for (size_t i = 0; i < N; ++i)
		{
			v1.push_back(url + to_string(i));
		}
	for (auto& str : v1)
	{
		bf.set(str);
	}
	// v2跟v1是相似字符串集（前缀⼀样），但是后缀不⼀样     
	  v1.clear();
	for (size_t i = 0; i < N; ++i)
	{
		string urlstr = url;
		urlstr += to_string(9999999 + i);
		v1.push_back(urlstr);
	}
	size_t n2 = 0;
	for (auto& str : v1)
	{
		if (bf.test(str)) // 误判
		{
			++n2;
		}
	}
	cout << "相似字符串误判率:" << (double)n2 / (double)N << endl;
		// 不相似字符串集前缀后缀都不⼀样        
		v1.clear();
	
		for (size_t i = 0; i < N; ++i)
		{
			string url = "zhihu.com";
			//string url = "孙悟空";
				url += to_string(i + rand());
			v1.push_back(url);
		}
	size_t n3 = 0;
	for (auto& str : v1)
	{
		if (bf.test(str))
		{
			++n3;
		}
	}
	cout << "不相似字符串误判率:" << (double)n3 / (double)N << endl;
	cout << "公式计算出的误判率: " << bf.getFalseProbability() << endl;
}

int main()
{
	TestBloomFilter2();
	//TestBloomFilter1();
	//testbf();
	//test_three_bitset();
	//cout << 127 / 32 << endl;
	//cout << 127 % 32 << endl;
	return 0;
}